#include <QFileInfo>
#include <QDir>
#include "MainWindow.h"
#include "ui_MainWindow.h"
//#include <QHBoxLayout>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //QHBoxLayout *mainLayout = new QHBoxLayout(ui->centralwidget);
    pc_FilaPowerForm = new FilaPowerForm(this);
    this->setCentralWidget(pc_FilaPowerForm);
     connect(pc_FilaPowerForm,&FilaPowerForm::sig_showMessage,this,&MainWindow::slot_showMessage);

     this->setWindowTitle("灯丝电源");


     QString documentsPath = QDir::tempPath();

     // 创建名为“20KWData”的文件夹
     QString folderName = "FilamentPower";
     QString folderPath = QDir(documentsPath).filePath(folderName);
     QDir dir(folderPath);

     if (!dir.exists()) {
         if (dir.mkdir(".")) {
             qDebug() << "文件夹创建成功：" << folderPath;
         } else {
             qDebug() << "文件夹创建失败";
         }
     } else {
         qDebug() << "文件夹已存在：" << folderPath;
     }

     // 获取并输出文件夹路径
     qDebug() << "Ini文件夹路径：" << folderPath;
 //    QString iniFilePath = QCoreApplication::applicationDirPath() + "/Settings.ini";
     QString iniFilePath = folderPath + "/Settings.ini";

     m_Settings = new QSettings(iniFilePath,QSettings::IniFormat);
    pc_FilaPowerForm->setIniFile(m_Settings);

    m_Style = m_Settings->value("Style/Color","black").toString();
    loadStyle(m_Style);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slot_showMessage(QString Msg)
{
    ui->statusbar->showMessage(Msg,2500);
}


void MainWindow::keyPressEvent(QKeyEvent *event)
{
    // 检查是否按下了 Ctrl+P
    if (event->key() == Qt::Key_O && event->modifiers() == Qt::ControlModifier) {
        qDebug() << "Ctrl+O 按键被捕获！";
        // 在这里执行你想要的操作
        if(m_Style != "black")
        {
            loadStyle("black");
            m_Style = "black";
            m_Settings->setValue("Style/Color",m_Style);
        }

    }
    else if (event->key() == Qt::Key_P && event->modifiers() == Qt::ControlModifier) {
        qDebug() << "Ctrl+P 按键被捕获！";

        // 在这里执行你想要的操作
        if(m_Style != "original")
        {
            loadStyle("original");
            m_Style = "original";
            m_Settings->setValue("Style/Color",m_Style);
        }
    }
    else if (event->key() == Qt::Key_Escape)
    {
        pc_FilaPowerForm->setWidgetFocus();
    }
    else
    {
        // 对于其他按键，调用基类的 keyPressEvent 方法
        QMainWindow::keyPressEvent(event);
    }
}

//样式
void MainWindow::loadStyle(QString currentStyle)
{
    QString styleFileName;
    if(currentStyle == "black")
    {
        styleFileName = ":/res/qss/style.css";
    }
    else if(currentStyle == "gray")
    {
        styleFileName = ":/res/qss/grayStyle.css";
    }
    else if(currentStyle == "white")
    {
        styleFileName = ":/res/qss/whiteStyle.css";
    }
    else if(currentStyle == "original")
    {
        qApp->setStyleSheet("");
        return;
    }
    else
    {
        return;
    }
    QString style;
    if (!QFileInfo::exists(styleFileName))
    {
        return;
    }
    QFile fh(styleFileName);
    if (fh.open(QFile::ReadOnly | QFile::Text))
    {
        style = fh.readAll();
        fh.close();
    }
    else
    {
        qWarning()<<"open file"<<styleFileName<<"error!";
    }
    qApp->setStyleSheet(style);
}


